package com.nuc.workflowserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nuc.workflowserver.common.util.CommonUtils;
import com.nuc.workflowserver.mapper.IFlowMapper;
import com.nuc.workflowserver.model.pojo.Flow;
import com.nuc.workflowserver.model.vo.FlowVO;
import com.nuc.workflowserver.service.IFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class IFlowServiceImpl implements IFlowService {

    @Autowired
    IFlowMapper iFlowMapper;

    @Override
    public PageInfo<Flow> selectPage(Flow flow, Integer pageNum,
            Integer pageSize) {
        return null;
    }

    @Override
    public List<Flow> selectList(Flow flow) {
        List<Flow> flows = iFlowMapper.selectList(flow);
        return flows;
    }

    @Override
    public Flow selectByPrimary(Flow flow) {
        return null;
    }

    @Override
    public Integer update(Flow flow) {
        return null;
    }

    @Override
    public Integer delete(Flow flow) {
        return null;
    }

    @Override
    public Integer insert(Flow flow) {
        return null;
    }

    @Override
    public Integer insertOrUpdateFlow(Flow flow) {
        if (null != flow && null != flow.getId() && !""
                .equals(flow.getId())) {  //更新
            flow.setUpdateTime(new Date());
            return iFlowMapper.update(flow);
        } else {  //新增
            flow.setId(CommonUtils.getUUID());
            flow.setCreateTime(new Date());
            flow.setUpdateTime(new Date());
            return iFlowMapper.insert(flow);
        }
    }

    @Override
    public PageInfo<FlowVO> selectFlowVOPage(Integer pageNum,
            Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<FlowVO> list = iFlowMapper.selectFlowVOPage();
        String str = "yyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        list.stream().forEach(el -> {
            if (null != el.getCreateTime()) {
                el.setCreateTimeStr(sdf.format(el.getCreateTime()));
            }
            if (null != el.getUpdateTime()) {
                el.setUpdateTimeStr(sdf.format(el.getUpdateTime()));
            }
        });
        PageInfo<FlowVO> page = new PageInfo<FlowVO>(list);
        return page;
    }
}
